package cn.biplam.front.template.control;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.Template;
import cn.biplam.front.template.base.ButtonImageLittle;
import cn.biplam.front.template.base.FieldSet;

/**
 * 树型导航栏，用于功能页面树型操作导航，例如数据仓库模型树型结构索引中选择一张表
 * @author XiaoRuxing 2009-5-29下午08:09:41
 * @version 1.0
 */
public class TreeNavigator implements Template{

	/** 导航栏Html对象 */
	private HtmlDiv navigatorDiv;
	/** 页面ID */
	private String ID;
	/** 导航栏菜单标题 */
	private String sTitle;
	/** 应用系统路径 */
	private String sHomeUrl;
	/** 用户信息 */
	private UserInfo mUser;
	/** 导航栏树 */
	private HtmlWindow tree;
	/** 折叠的脚本命令 */
	private String shrinkScript;
	/** 展开的脚本命令 */
	private String expandScript;
	/** 是否可折叠 */
	private boolean haveShrink=false;
	/** 导航栏宽度 */
	private String width="197";
	private HtmlText div;
	
	private String fieldSetID;
	private String shrinkID;
	private String expandID;
	private String title2ID;
	
	
	/**
	 * 创建树导航栏
	 * @param ID       页面ID
	 * @param sTitle   导航栏标题
	 * @param sHomeUrl 应用系统路径 
	 * @param mUser    用户信息
	 * @throws ErrorMessage
	 */
	public TreeNavigator(String ID,String sTitle,String sHomeUrl,UserInfo mUser) throws ErrorMessage{
		if(ID==null||"".equals(ID)){
			throw new ErrorMessage("Navigator id is null!");
		}
		this.ID=ID;
		this.sTitle=sTitle;
		this.sHomeUrl=sHomeUrl;
		this.mUser=mUser;
		this.fieldSetID=ID+"D";
		this.shrinkID=ID+"S";
		this.expandID=ID+"E";
		this.title2ID=ID+"T";
		this.div=new HtmlText(null,"ent");
	}

	/**
	 * 设置导航栏树
	 * @param tree 导航栏树
	 */
	public void setTree(HtmlWindow tree){
		this.tree=tree;
	}

	/**
	 * 设置折叠和展开的脚本命令
	 * @param shrinkScript 折叠按钮点击脚本命令
	 * @param expandScript 展开按钮点击脚本命令 
	 */
	public void setBind(String shrinkScript,String expandScript){
		this.shrinkScript=shrinkScript;
		this.expandScript=expandScript;
		this.haveShrink=true;
	}
	
	/**
	 * 设置导航栏的显示宽度(单位值PX) 
	 * @param width 宽度值
	 */
	public void setWidth(String width){
		this.width=width;
	}
	
	private void _createNavigator() throws ErrorMessage{
		this.navigatorDiv = new HtmlDiv(this.ID,"mdnd");
		FieldSet navigator=new FieldSet(this.fieldSetID,this.sTitle);
		if(this.haveShrink){
			_createShrink();
		}
		navigator.put(div);
		navigator.put(tree);
		this.navigatorDiv.setStyleWidth(this.width);
		this.navigatorDiv.put(navigator.get());
	}
	
	private void _createShrink() throws ErrorMessage{
		
		/** 导航栏展开时界面 */
		HtmlWindow button = new ButtonImageLittle(this.shrinkID,sHomeUrl+"/images/retracting.gif").get();
		button.setPositionMode(Position_Mode.Absolute);
		button.setLeft(String.valueOf(Integer.valueOf(this.width)-15));
		button.setMarginTop("-2");
		button.setTitle(CaptionComp.get(mUser,"221"));
		button.bind(this.shrinkID,Window_Event.OnClick,"$('"+this.fieldSetID+"').style.visibility='hidden'; ");
		button.bind(this.shrinkID,Window_Event.OnClick,"$('"+this.title2ID+"').style.visibility='inherit'; ");
		button.bind(this.shrinkID,Window_Event.OnClick,"$('"+this.shrinkID+"').style.visibility='hidden'; ");
		button.bind(this.shrinkID,Window_Event.OnClick,"$('"+this.ID+"').style.width='25px'; ");
		if(shrinkScript!=null||!"".equals(shrinkScript)){
			button.bind(this.shrinkID,Window_Event.OnClick,this.shrinkScript);
		}

		/**导航栏隐藏时界面*/
		HtmlDiv navigator_title2=new HtmlDiv(this.title2ID,"mdnt2");
		this.navigatorDiv.put(navigator_title2);
		HtmlWindow button2 = new ButtonImageLittle(this.expandID,sHomeUrl+"/images/spread.gif").get();
		button2.setMarginRight("3");
		navigator_title2.put(button2);
		button2.setTitle(CaptionComp.get(mUser,"222"));
		button2.bind(this.expandID,Window_Event.OnClick,"$('"+this.title2ID+"').style.visibility='hidden';");
		button2.bind(this.expandID,Window_Event.OnClick,"$('"+this.shrinkID+"').style.visibility='inherit';");
		button2.bind(this.expandID,Window_Event.OnClick,"$('"+this.fieldSetID+"').style.visibility='inherit';");
		button2.bind(this.expandID,Window_Event.OnClick,"$('"+this.ID+"').style.width='"+this.width+"';");
		if(expandScript!=null||!"".equals(expandScript)){
			button.bind(this.expandID,Window_Event.OnClick,this.expandScript);
		}
		this.navigatorDiv.put(button);
	}
	
	/**
	 * 返回创建好的HtmlWindow对象
	 * @return HtmlWindow对象
	 * @throws ErrorMessage
	 */
	public HtmlWindow get() throws ErrorMessage{
		this._createNavigator();
		return this.navigatorDiv;
	}
	
}
